import { isEmpty, isFalsy } from '@src/util/type';
/* model */
import { BaseSelectUserDisabledSelectId, BaseSelectUserTypeIdEnum, SelectUserComponentRole, SelectUserProjectTeam, SelectUserComponentUser } from '@src/component/common/BaseSelectUser/model'
/* types */
import { BaseSelectUserSelectedItemType } from '@src/component/common/BaseSelectUser/types'
/* util */
import { isFalse, isNotEmpty } from '@src/util/type'
/* api */
import { getSelectGetUserByIdList } from '@src/component/common/BaseSelectUser/api'
import MsgModel from '@model/MsgModel';

/** 
 * @description 是否为已选择 部门类型
*/
function isDepartmentSelectType(data: BaseSelectUserSelectedItemType): boolean {
  return data?.typeId == BaseSelectUserTypeIdEnum.Department
}

/** 
 * @description 是否为已选择 团队类型
*/
function isTagSelectType(data: BaseSelectUserSelectedItemType): boolean {
  return data?.typeId == BaseSelectUserTypeIdEnum.Tag
}

/** 
 * @description 是否为已选择 用户类型
*/
function isUserSelectType(data: BaseSelectUserSelectedItemType): boolean {
  return data?.typeId == BaseSelectUserTypeIdEnum.User
}

/** 
 * @description 是否为已选择 角色类型
*/
function isRoleSelectType(data: BaseSelectUserSelectedItemType): boolean {
  return data?.typeId == BaseSelectUserTypeIdEnum.Role
}

/** 
 * @description 是否为已选择 服务商类型
*/
function isServiceProviderSelectType(data: BaseSelectUserSelectedItemType): boolean {
  return data?.typeId == BaseSelectUserTypeIdEnum.ServiceProvider
}

/** 
 * @description 是否为已选择 动态获取 类型
*/
function isDynamicSelectType(data: BaseSelectUserSelectedItemType): boolean {
  return data?.typeId == BaseSelectUserTypeIdEnum.Dynamic
}

/** 
 * @description 是否为已选择的一项 的 id
*/
function getCheckedItemId(item: Record<string, any>): string {
  return item?.id || item?.userId || ''
}

/** 
 * @description 是否为已选择的一项 的 名称
*/
function getCheckedItemName(item: Record<string, any>): string {
  return item?.name || item?.displayName || ''
}

/** 
 * @description 是否为已选择的一项 的 图片，用于显示
*/
function getCheckedItemImage(item: Record<string, any>): string {
  return item?.head || item?.logo || ''
}

/** 
 * @description 是否为已选择的一项 的 类型 id
*/
function getCheckedItemTypeId(item: Record<string, any>): string {
  return item?.typeId || ''
}

/** 
 * @description 是否为已选择的一项 的 staffDd, 主要用户 企微端 显示人员名称
*/
function getCheckedItemStaffId(item: Record<string, any>): string {
  return item?.staffId || ''
}



/** 
 * @description 已选择的工单角色标签
*/
function getCheckedItemUserTypeList(item: Record<string, any>): any {
  return item?.userTypeList || []
}

/** 
 * @description 是否禁用
*/
function getCheckedItemDisabled(item: Record<string, any>): any {
  return Boolean(item?.disabled)
}
/** 
 * @description 是否为禁用选择的一项
*/
function isDisabledSelectItem(item: BaseSelectUserSelectedItemType) {
  
  const id = getCheckedItemId(item)
  
  return String(id) === BaseSelectUserDisabledSelectId
}

async function checkCompletionUserListData(items: BaseSelectUserSelectedItemType[]) {
  
  const users: SelectUserComponentUser[] = items.filter(isUserSelectType) as SelectUserComponentUser[]
  let needCompletionUserList = []
  
  if (isEmpty(users)) {
    return null
  }
  
  needCompletionUserList = users.filter(item => {
    return isFalsy(item?.displayName) || isFalsy(item?.head) || isFalsy(item?.staffId)
  })
  
  const userIds = needCompletionUserList.map(item => getCheckedItemId(item)).filter(isNotEmpty)
  
  return (
    getSelectGetUserByIdList(userIds).then(res => {
      
      if (MsgModel.isFail(res)) {
        return null
      }
      
      const remoteUsers = res.data || []
      
      const remoteUserIdsMap = remoteUsers.reduce((acc, item) => {
        acc[item.userId] = item
        return acc
      }, {} as Record<string, SelectUserComponentUser>)
      
      const remoteUserStaffIdsMap = remoteUsers.reduce((acc, item) => {
        acc[getCheckedItemStaffId(item)] = item
        return acc
      }, {} as Record<string, SelectUserComponentUser>)
      
      // 不需要深拷贝
      let newItems = items.slice()
      
      newItems = newItems.map(item => {
        
        if (isUserSelectType(item)) {
          
          const staffId = getCheckedItemStaffId(item)
          const userId = getCheckedItemId(item)
          
          item = Object.assign(
            {}, 
            item,
            staffId ? remoteUserStaffIdsMap[staffId] || {} : {},
            userId ? remoteUserIdsMap[userId] || {} : {}
          )
          
        }
        
        return item
      })
      
      return newItems
      
    })
  )
  
}

function getDefaultSelectedRoleByRoleList(roleList: SelectUserComponentRole[]) {
  
  // 1. 首先判断是否是客服云还是服务云
  const firstRole = roleList[0]
  
  if (isFalsy(firstRole)) {
    return null
  }
  
  const { isCustomerServiceCloud, isServiceCloud } = firstRole
  // 非客服云和服务云，直接返回第一个角色
  if (isFalse(isCustomerServiceCloud) && isFalse(isServiceCloud)) {
    return firstRole
  }
  
  // 2. 如果是客服云或者服务云，则返回其默认角色的第一个
  const childrenRoles = firstRole?.children || firstRole?.tagChildren || []
  if (isEmpty(childrenRoles)) {
    return firstRole
  }
  
  // 3. 客服云或者服务云 子级的 第一个角色
  const childrenFirstRole = childrenRoles[0]
  if (isFalsy(childrenFirstRole)) {
    return firstRole
  }
  
  return childrenFirstRole
  
}

function getDefaultSelectProjectTeamList(projectTeamList: SelectUserProjectTeam[]) {
  const firstProjectTeam = projectTeamList[0]

  if (isFalsy(firstProjectTeam)) {
    return null
  }

  return firstProjectTeam
}
export {
  getCheckedItemId,
  isDepartmentSelectType,
  isTagSelectType,
  isUserSelectType,
  isRoleSelectType,
  isServiceProviderSelectType,
  isDynamicSelectType,
  isDisabledSelectItem,
  getCheckedItemName,
  getCheckedItemImage,
  getCheckedItemTypeId,
  getCheckedItemStaffId,
  getCheckedItemUserTypeList,
  getCheckedItemDisabled,
  checkCompletionUserListData,
  getDefaultSelectedRoleByRoleList,
  getDefaultSelectProjectTeamList
}
